package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.base.util.StringUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.vo.WaitaffirmVO;
import com.jishuo.nontax.bfc.service.WaitaffirmService;
import org.dom4j.Element;

import java.math.BigDecimal;

/**
 * @author hlp
 * @date 2023/2/2 10:42
 * @Description:  支出 业务层
 */
public class WaitaffirmApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if (head.equals("setWaitaffirm")) {
            WaitaffirmVO vo = new WaitaffirmVO();
            vo.setPayer(body.elementText("payer"));
            vo.setPayeraccount(body.elementText("payeraccount"));
            vo.setPayerbank(body.elementText("payerbank"));
            vo.setAccountcode(body.elementText("accountcode"));
            vo.setPaybankdt(body.elementText("paybankdt"));
            vo.setBankaffirm(body.elementText("bankaffirm"));
            vo.setBankstation(body.elementText("bankstation"));
            vo.setOriginfund(new BigDecimal(body.elementText("originfund")));
            vo.setPaywaycode(body.elementText("paywaycode"));
            vo.setPostscript(body.elementText("postscript"));
            WaitaffirmService waitaffirmService = (WaitaffirmService) SpringContextUtil.getBean("waitaffirmService");
            BaseResult ret = waitaffirmService.setWaitaffirm(vo);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;

        } else if (head.equals("sendWaitaffirm")) { // 根据 银行编号、银行收款时间 查询银行上传的待查信息
            String accountcode = body.elementText("accountcode");
            String paybankdt = body.elementText("paybankdt");

            if(accountcode==null||accountcode.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }
            WaitaffirmService waitaffirmService = (WaitaffirmService) SpringContextUtil.getBean("waitaffirmService");
            BaseResult ret = waitaffirmService.sendWaitAffirm(accountcode, paybankdt);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;

        } else if (head.equals("cleanWaitaffirm")) { //  根据缴款人账户和账户收款时间 把收款账户,缴款人账户,缴款时间上传
            String accountcode = body.elementText("accountcode");
            String paybankdt = body.elementText("paybankdt");

            if(StringUtil.isEmpty(accountcode) || StringUtil.isEmpty(paybankdt) || paybankdt.length() < 10){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数错误");
            }

            WaitaffirmService waitaffirmService = (WaitaffirmService) SpringContextUtil.getBean("waitaffirmService");

            BaseResult ret = waitaffirmService.cleanWaitaffirm(accountcode, paybankdt.substring(0, 10), 0);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;

        }else if(head.equals("regetWaitaffirm")){ //获取可用的待查信息 ,向财政获取对账成功的待查信息
            WaitaffirmVO waitaffirmVO = new WaitaffirmVO();
            waitaffirmVO.setAccountcode(body.selectSingleNode("//root/body/accountcode").getText());
            waitaffirmVO.setBankaffirm(body.selectSingleNode("//root/body/bankaffirm").getText());
            waitaffirmVO.setPaybankdt(body.selectSingleNode("//root/body/paybankdt").getText());

            if(waitaffirmVO.getAccountcode()==null||waitaffirmVO.getAccountcode().equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }

            WaitaffirmService waitaffirmService = (WaitaffirmService) SpringContextUtil.getBean("waitaffirmService");
            return waitaffirmService.regetWaitaffirm(waitaffirmVO);

        }else if(head.equals("resetWaitaffirmInfo")){ //修改证书配置表信息
            WaitaffirmVO waitaffirmVO = new WaitaffirmVO();
            waitaffirmVO.setPayer(body.selectSingleNode("//root/body/payer").getText());
            waitaffirmVO.setPayeraccount(body.selectSingleNode("//root/body/payeraccount").getText());
            waitaffirmVO.setPayerbank(body.selectSingleNode("//root/body/payerbank").getText());
            waitaffirmVO.setPostscript(body.selectSingleNode("//root/body/postscript").getText());
            waitaffirmVO.setAccountcode(body.selectSingleNode("//root/body/accountcode").getText());
            waitaffirmVO.setBankaffirm(body.selectSingleNode("//root/body/bankaffirm").getText());
            waitaffirmVO.setPaybankdt(body.selectSingleNode("//root/body/paybankdt").getText());

            WaitaffirmService waitaffirmService = (WaitaffirmService) SpringContextUtil.getBean("waitaffirmService");
            BaseResult ret = waitaffirmService.resetWaitaffirmInfo(waitaffirmVO);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }


        return null;
    }
}
